// 工具函数: 计算两个日期之间的年份和月份差
export const CalculateYearsAndMonths = (startDate, endDate) => {
    const start = new Date(startDate);
    const end = new Date(endDate);
    const years = end.getFullYear() - start.getFullYear();
    const months = end.getMonth() - start.getMonth();
   
    return { years, months };
}

// 工具函数: 通过 年/月 计算 开始时间/结束时间
export const  GetMonthDates = (years, months) => {
    debugger;
     var nowTime = new Date();
     var nowTimeS = nowTime.getTime();
     var preTime = years * 365 * 24 * 60 * 60 * 1000 + months * 30 * 24 * 60 * 60 * 1000;
     var endDate = nowTime.getFullYear() + "-" + (nowTime.getMonth()+1 < 10 ? "0" + (nowTime.getMonth()+1) : nowTime.getMonth()+1) + "-" + (nowTime.getDate() < 10 ? "0"+nowTime.getDate() : nowTime.getDate());
     var oldTime = new Date(nowTimeS - preTime);
     var startDate = oldTime.getFullYear() + "-" + (oldTime.getMonth()+1 < 10 ? "0"+(oldTime.getMonth()+1) : oldTime.getMonth()+1) + "-" + (oldTime.getDate() < 10 ? "0"+oldTime.getDate() : oldTime.getDate());
    return {
      start: startDate,
      end: endDate
    };  
}

// 工具函数: 将时间戳转换为 yyyy-mm-dd
export const FormatDate = (dateTime) =>{
    var date = new Date(dateTime);
    var YY = date.getFullYear();
    var MM = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    return YY + '-' + MM + '-' + DD;
}

// 工具函数: 获取url携带的参数
export const GetRequest = (value) => {
    var url = decodeURI(window.location.search); //?id="123456"&name="www";
    var object = {};
    if (url.indexOf("?") != -1) {  //url中存在问号，也就说有参数。
      var str = url.substr(1);     //得到?后面的字符串
      var strs = str.split("&");   //将得到的参数分隔成数组[id="123456",name="www"];
      for (var i = 0; i < strs.length; i++) {
        object[strs[i].split("=")[0]] = strs[i].split("=")[1];//得到{id:'123456',name:'www'}
      }
    }
    return object[value];
}

// 工具函数: 获取url携带的参数
export  function getUrlKey(name){
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
}